package com.content;

import java.net.URL;
import java.util.List;

import net.htmlparser.jericho.CharacterReference;
import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.MasonTagTypes;
import net.htmlparser.jericho.MicrosoftTagTypes;
import net.htmlparser.jericho.PHPTagTypes;
import net.htmlparser.jericho.Source;

public class HtmlParser {
	
	private String PRODUCT_NAME="";
	
	//HINH THUC KHUYEN MAI
	private String PRODUCT_TYPE="";
	
	//GIA THI TRUONG
	private String PRODUCT_PRICE="";
	
	//GIAM GIA
	private String PRODUCT_PRICE_SALE="";
	
	//NGAY BAT DAU KHUYEN MAI
	private String PRODUCT_SALE_DATE_BEGIN="";
	
	//NGAY KET THUC KHUYEN MAI
	private String PRODUCT_SALE_DATE_END="";
	
	//PHAM VI KHUYEN MAI
	private String PRODUCT_DOMAIN="";
	
	
	public static void main(String[] args) {
		try {
			
			HtmlParser htmlParser = new HtmlParser();
			
			String sourceUrl="";
			Source source = null;			
			
			sourceUrl = "http://tinkhuyenmai.net/Product.aspx?ID=13394";
			//sourceUrl = "http://livescore.com/soccer/england/";		
			//MicrosoftTagTypes.register();
			//PHPTagTypes.register();
			//PHPTagTypes.PHP_SHORT.deregister(); // remove PHP short tags for this example otherwise they override processing instructions
			//MasonTagTypes.register();
			
			source=new Source(new URL(sourceUrl));
			source.fullSequentialParse();
			
			//set info product
			htmlParser.setProductName(source);
			htmlParser.setProductType(source,0);
			
			//get info product
			System.out.println("Name: "+htmlParser.getProductName());
			System.out.println("Type: "+htmlParser.getProductType());
			
			
			//System.out.println("Tên sản phẩm: "+ htmlParser.getProductName(source));
			
			//System.out.println("Hình thức khuyến mãi: "+htmlParser.getSaleType(source,0));
			
			
			//List<Element> img = source.getAllElements("id", "photo_1", false);
			//System.out.println(img.get(0));
			
			
			
			
//			int size = tbl.size();		
//			
//			Element e = null;
//			
//			for (int it=0;it<size;it++) {
//				if (it<tbl.size())
//					e = (Element)tbl.get(it);
//				else 
//					e = null;
//				if (e!=null) {			
//					content = CharacterReference.decodeCollapseWhiteSpace(e.getContent());					
//					//content = content.replaceAll("<b>","").replaceAll("</b>","").trim();
//					//content = content.substring(28);
//					System.out.println(content);
//				}
//			}
			
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}
	
	public void setProductName(Source source)
	{
		try {
			List<Element> tbl = source.getAllElements("class", "title_txt", false);
			Element e = (Element)tbl.get(0);
			
			//value = <span style=" font:normal;">Tên sản phẩm :</span> Bình nóng lạnh Rossi 20l <span style=" font:normal; color:#cc9900; font-size:11px"> 
			String value = CharacterReference.decodeCollapseWhiteSpace(e.getContent());
			
			String tam="";
			String productName="";
			
			//tam = Bình nóng lạnh Rossi 20l <span style=" font:normal; color:#cc9900; font-size:11px">
			tam = value.substring(value.indexOf("</span>")+7);
			
			//productName = Bình nóng lạnh Rossi 20l
			productName = tam.substring(0,tam.indexOf("<span "));
			
			this.PRODUCT_NAME = productName.trim();
			
		} catch (Exception e) {
			this.PRODUCT_NAME = "FALSE";
		}
	}
	
	public String getProductName(){
		return this.PRODUCT_NAME;
	}
	
	
	public void setProductType(Source source, int p_index){
		try {
			String productType="";
			
			productType = getInfoProduct(source, p_index);			
			
			this.PRODUCT_TYPE = productType.trim();
			
		} catch (Exception e) {
			this.PRODUCT_TYPE="FALSE";
		}
		
	}
	
	public String getProductType(){
		
		return this.PRODUCT_TYPE;
	}	
	
	private String getInfoProduct(Source source,int p_index){
		try {
			String value ="";
			Element e =null;
			List<Element> tbl = source.getAllElements("style", "color:#CC0000", false);
			int size = tbl.size();
			
			if(p_index > size) return "FALSE";
			
			e = (Element)tbl.get(p_index);
			value = CharacterReference.decodeCollapseWhiteSpace(e.getContent());
			
//			
//			for (int it=0;it<size;it++) {
//				if (it<tbl.size()){
//					e = (Element)tbl.get(it);				
//					value = CharacterReference.decodeCollapseWhiteSpace(e.getContent());			
//					System.out.println(value);
//				}
//			}			
			return value;
			
		} catch (Exception e) {	
			e.printStackTrace();
			return "FALSE";
		}
	}
	
}
